<?php
defined('IN_IA') or exit('Access Denied');

$ops = array('cabinet_admin','update_qrcode');
$op_names = array('柜子与管理员');
$op = in_array($op, $ops) ? $op : 'cabinet_admin';
$gettime = $this -> module['config']['gettime'];//自动签收时间
$uniacid = $_W['uniacid'];
$status = $_GPC['status'];
$rolenamearray =array(
    'admin'=>'管理员',
    'teacher'=>'教师',
    'student'=>'课代表'
);



    if($_GPC['device_info_id']!=''){
        $type = array(intval($_GPC['device_info_id']));
        $device = $this->all_device_info($type);
    }else{
        $type = 0;
        $device = $this->all_device_info($type);
    }
	$device_info_select = array();
    foreach($device as $key=>$value){
        $device_info_select[] = array('device_info_id' => $value['device_info_id']);
    }
if($op == 'cabinet_admin' ){
	$_W['page']['title'] = '柜子与管理员';

	//$condition = "  uniacid = :uniacid";
	//$paras = array(':uniacid' => $_W['uniacid']);
    if($_GPC['device_info_id']!=''){
        foreach($device as $key=>$value){//条件过滤
            if($value['device_info_id']!=$_GPC['device_info_id'])
            unset($device[$key]);
        }        
    }

    $pindex = max(1, intval($_GPC['page']));
	$psize = 10;
    $tatal = count($device);
    $start = ($pindex - 1) * $psize;
    $end = $start + $psize - 1;    
    foreach($device as $key=>$value){//留下这页的信息
        if($key<$start|$key>$end)
        unset($device[$key]);
    }
    
    foreach($device as $key=>$value){//进入柜子与管理员表
        $device_info_id = $value['device_info_id'];
        // $sql = "select  * from " . tablename('homework_cabinet_admin') . " where $condition and device_info_id='{$device_info_id}'";
        // $info = pdo_fetchall($sql, $paras);
        //pdo_get应该是微擎写的，数据库表自动加前缀，参数自动转换成绑定参数
        $info = pdo_get('homework_cabinet_admin', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $device_info_id), array('userid', 'remark', 'time' ,'random','device_name','door_num'));
        $userid = explode(',',$info['userid']);//如果为空字符串，也会返回一个数组，Array ( [0] => )
        $device[$key]['userid'] = $userid;
        $device[$key]['remark'] = $info['remark'];
        $device[$key]['time'] = $info['time']; 
        $device[$key]['random'] = $info['random']; 
        $device[$key]['device_name'] = $info['device_name']; 
        $device[$key]['door_num'] = $info['door_num'];         
    }
    foreach($device as $key=>$value){
        if(empty($value['userid'])) continue;
        foreach($value['userid'] as $k =>$val){
            $info = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] , 'id' => $val), array('openid', 'role', 'name'));
            $device[$key]['openid'][] = $info['openid'];
            $device[$key]['role'][] = $info['role'];
            $device[$key]['name'][] = $info['name'];
        }
    }
    
    //print_r(count($device[0]['userid']));
    $pager = pagination($total, $pindex, $psize);
    include wl_template('bind/cabinet_admin');
	exit;
}
if($op == 'update_qrcode' ){   
    $info = $_POST['info_id'];
    $time = time();
    $random = $this->return_random();
    
    $ifexist = pdo_get('homework_cabinet_admin', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info) ,array('random'));
    if($ifexist==null){
        //print_r(111);
        $arr = array('time'=>$time ,'random'=>$random ,'uniacid'=>$_W['uniacid'] ,'device_info_id'=>$info);
        pdo_insert('homework_cabinet_admin' , $arr);       
    }
    else{
        $arr = array('time'=>$time ,'random'=>$random);
        pdo_update('homework_cabinet_admin' , $arr ,array('device_info_id'=>$info));  
    }
 
    //print_r(json_encode($ifexist));
}